/*
 * BSQWriter.java
 *
 * Created on September 20, 2004, 7:23 PM
 */
package edu.temple.GUS.MMA.io;
import edu.temple.GUS.MMA.DataTypes.MMAGrid;
import edu.temple.GUS.MMA.DataTypes.MMAHyperCube;
import edu.temple.GUS.MMA.DataTypes.MMASpaceCube;
import edu.temple.GUS.MMA.DataTypes.MMATimeCube;
import edu.temple.GUS.MMA.DataTypes.MMATimeSeries;
import java.io.*;
import java.util.*;

/**
 *
 * @author  rkhanna
 */
public class BSQWriter 
{
    
    /** Creates a new instance of BSQWriter */
    public BSQWriter()     
    {
    }

    public static void  writeGrid(MMAGrid grid, String path)
    {
        byte b[];    
        String s="";          
        float arr[][]=grid.getData();      
        int X = grid.getX();
        int Y = grid.getY();        
        try
        {            
            String file= new String(path+"header.hdr");     
            FileOutputStream fos = new FileOutputStream(file, true );                              
            PrintWriter pw = new PrintWriter(fos, true);
            pw.println("grid");               
            pw.println(grid.getX());               
            pw.println(grid.getY());                           
            pw.println(0);               
            pw.println(0);               
            pw.println(grid.getXResolution());               
            pw.println(grid.getYResolution());               
            pw.println(0);               
            pw.println(0);                           
            pw.println(grid.getNoData());                           
            pw.println(grid.getXLL());                           
            pw.println(grid.getYLL());                           
            pw.close();
            fos.close();
            
            file = path+"1.dat";          
            fos = new FileOutputStream(file, true );                              
            pw = new PrintWriter(fos, true);
            for(int j=0;j<Y;j++)
            {    
               for(int i=0;i<X;i++)
                   pw.print(arr[j][i]+"  ");               
             pw.println();
             }      
            pw.close();
            fos.close();
         } catch(Exception e) { System.out.println(e+ "Exception thrown");  }                      
    }//end of writeGrid
    
    
    
    public static void writeTimeCube(MMATimeCube timecube, String path)
    {
        byte b[];    
        String s="";    
        String temppath;
        float arr[][][]=timecube.getData();      
        int x = timecube.getX();
        int y = timecube.getY();
        int t = timecube.getT();
        FileOutputStream fos;
        PrintWriter pw;
        System.out.println(x);
        System.out.println(y);
        System.out.println(t);        
        String file= new String(path+"header.hdr");     
        try
        {               
        fos = new FileOutputStream(file, true );                              
        pw = new PrintWriter(fos, true);
        pw.println("timecube");               
        pw.println(timecube.getX());               
        pw.println(timecube.getY());                           
        pw.println(0);               
        pw.println(timecube.getT());               
        pw.println(timecube.getXResolution());               
        pw.println(timecube.getYResolution());               
        pw.println(0);               
        pw.println(timecube.getTResolution());                           
        pw.println(timecube.getNoData());                           
        pw.println(timecube.getXLL());                           
        pw.println(timecube.getYLL());                           
        fos.close();
        pw.close();
        }catch(Exception e){System.out.println("Exception thrown from writetimecube()");}
        
           int filecount;
           for(int k=0;k<t;k++)
           {
                try
                {               
                    filecount=k+1;                    
                    File f = new File(path);  
                    f.mkdir();
                    temppath=path+filecount+".dat";                    
                    System.out.println("Creating file::"+ temppath);
                    fos = new FileOutputStream(temppath, true );                 
                    pw = new PrintWriter(fos, true);                    
                    
                    for(int j=0;j<y;j++)
                    {
                        for(int i=0;i<x;i++)
                             pw.print(arr[k][j][i]+"  ");
                     pw.println();
                    } 
                    fos.close();
                    pw.close();
                }catch(Exception e) { System.out.println(e + "Exception thrown"); }                        
            }// end of outer for loop
    }// end of writeTimeCube
    

    public static void writeSpaceCube(MMASpaceCube spacecube, String path)
    {
        byte b[];    
        String s="";        
        String temppath;
        float arr[][][]=spacecube.getData();      
        int x = spacecube.getX();
        int y = spacecube.getY();
        int z = spacecube.getZ();
        int filecount;
        FileOutputStream fos;
        PrintWriter pw; 
        String file= new String(path+"header.hdr");     
        try
        {                       
        fos = new FileOutputStream(file, true );                              
        pw = new PrintWriter(fos, true);
        pw.println("spacecube");               
        pw.println(spacecube.getX());               
        pw.println(spacecube.getY());                           
        pw.println(spacecube.getZ());               
        pw.println(0);                       
        pw.println(spacecube.getXResolution());               
        pw.println(spacecube.getYResolution());               
        pw.println(spacecube.getZResolution());                           
        pw.println(0);          
        pw.println(spacecube.getNoData());                           
        pw.println(spacecube.getXLL());                           
        pw.println(spacecube.getYLL());                           
        fos.close();
        pw.close();
        }catch(Exception e){System.out.println("Exception thrown from writeSpacecube()");}        
           for(int k=0;k<z;k++)
           {
                try
                {               
                    filecount=k+1;
                    temppath=path+filecount+".dat";
                    fos = new FileOutputStream(temppath, true );                 
                    pw = new PrintWriter(fos, true);
                    for(int j=0;j<y;j++)
                    {
                        for(int i=0;i<x;i++)
                         pw.print(arr[k][j][i]+"  ");
                     pw.println();
                    } 
                    fos.close();
                    
                    pw.close();
                }catch(Exception e) { System.out.println(e + "Exception thrown"); }        
            }
    }// end of writeSpaceCube
    
    
    public static void writeHyperCube(MMAHyperCube hypercube, String path)
    {
        System.out.println("Writing HyperCube.......");        
        byte b[];    
        String s="";        
        String temppath;        
        String filepath="";
        float arr[][][][]=hypercube.getData();      
        int x = hypercube.getX();
        int y = hypercube.getY();
        int z = hypercube.getZ();
        int t = hypercube.getT();
        int filecount=0;
        int dircount;
        FileOutputStream fos;
        PrintWriter pw; 
        String file= new String(path+"header.hdr");     
        try{          
        fos = new FileOutputStream(file, true );                              
        pw = new PrintWriter(fos, true);
        pw.println("hypercube");               
        pw.println(hypercube.getX());               
        pw.println(hypercube.getY());                           
        pw.println(hypercube.getZ());               
        pw.println(hypercube.getT());                       
        pw.println(hypercube.getXResolution());               
        pw.println(hypercube.getYResolution());               
        pw.println(hypercube.getZResolution());                           
        pw.println(hypercube.getTResolution());                           
        pw.println(hypercube.getNoData());                           
        pw.println(hypercube.getXLL());                           
        pw.println(hypercube.getYLL());                           
       // pw.println(hypercube.getTResolution());         
        fos.close();
        pw.close();
        }catch(Exception e){System.out.println("Exception thrown from writehypercube()");}        
        System.out.println(x);
        System.out.println(y);
        System.out.println(z);
        System.out.println(t);        
           for(int l=0;l<t;l++)
           {
            dircount=l+1;
            temppath=path+dircount;
            File f = new File(temppath);           
            f.mkdir();
               for(int k=0;k<z;k++)
               {    
                try
                {    
                    filecount++;
                    filepath=temppath+"\\"+filecount+".dat";
                    fos = new FileOutputStream(filepath, true );
                    pw = new PrintWriter(fos, true);
                    for(int j=0;j<y;j++)
                    {
                        for(int i=0;i<x;i++)
                         pw.print(arr[l][k][j][i]+"  ");
                     pw.println();
                    }                     
                fos.close();
                pw.close();
                }catch(Exception e) { System.out.println(e + "Exception thrown from writeHyperCube"); }        
               } 
            }
     }// end of writeHyperCube
    
    
        public static void writeTimeSeries(MMATimeSeries ts, String path)
    {
        byte b[];    
        String s="";    
        String temppath;
        float arr[]=ts.getData();      
        FileOutputStream fos;
        PrintWriter pw;
        String file= new String(path+"header.hdr");     
        try
        {               
        fos = new FileOutputStream(file, true );                              
        pw = new PrintWriter(fos, true);
        pw.println("timeseries");                         
        pw.println(0);               
        pw.println(0);               
        pw.println(0);               
        pw.println(ts.getT());               
        pw.println(0);               
        pw.println(0);               
        pw.println(0);               
        pw.println(ts.getTResolution());               
        pw.println(ts.getNoData());                           
        pw.println(ts.getXLL());                           
        pw.println(ts.getYLL());                           
        
        //pw.println(timecube.getTResolution());                           
        fos.close();
        pw.close();
        }catch(Exception e){System.out.println("Exception thrown from writetimeseries()");}

         try
         {                          
           temppath=path+"1.dat";                    
           System.out.println("HAHA"+ temppath);
           fos = new FileOutputStream(temppath, true );                 
           pw = new PrintWriter(fos, true);              
           for(int k=0;k<arr.length;k++)
           {
                    float temp1 = arr[k];
                    System.out.println("temp1 :" + temp1 );
                    pw.println(temp1 +"  ");                    
                    pw.flush();
            }// end of outer for loop
            fos.close();
            pw.close();
         }catch(Exception e) { System.out.println(e + "Exception thrown from write TimeSeries write function"); }                                      
    }// end of writeTimeSeries
}// end of class    
